#include <burst_kernel.h>

static bus_module_desc_t bus_win32_base_io_desc = {
	BUS_MODULE_IO, 
	"WIN32 base IO module", 
	"",
	"",
	""
};

static int bus_init()
{
	WSADATA w;
	return WSAStartup(&w, 0x0202);
}

static int bus_socket(int af, int stype, int proto)
{
	int sockfd = socket(af, stype, proto);
	if(sockfd < 0) 
		bus_panic(__FUNC__, __FILE__, __LINE__, 51280, "No permission or not enough resource.\n\t"
								"Operation finished.");
	return sockfd;
}

static int bus_set_non_block(int sockfd, int flag)
{
	return ioctlsocket(sockfd, FIONBIO, &flag);
}

static int bus_connect(int sockfd, struct sockaddr *sa, int unused)
{
	return connect(sockfd, sa, sizeof(struct sockaddr));
}

static int bus_bind(int sockfd, struct sockaddr *sa, int unused)
{
	return bind(sockfd, sa, sizeof(struct sockaddr));
}

static int bus_listen(int sockfd, int backlog)
{
	return listen(sockfd, backlog);
}

static int bus_accept(int sockfd, struct sockaddr *sa, int *unused)
{
	int size = sizeof(struct sockaddr);
	return accept(sockfd, sa, &size);
}

static int bus_send(int sockfd, char *buf, int len, int flag)
{
	return send(sockfd, buf, len, flag);
}

static int bus_recv(int sockfd, char *buf, int len, int flag)
{
	return recv(sockfd, buf, len, flag);
}


bus_io_t bus_win32_base_io = {
	&bus_win32_base_io_desc, 
	&bus_init, 
	&bus_set_non_block, 

};